﻿using System;
using System.Collections.Generic;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryPatient : ADONetRepositoryBase<Patient> 
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Patient entity)
        {
            var cmd = new SqlCommand("PatientInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Surname", entity.Surname));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("Middlename", entity.Middlename));
            cmd.Parameters.Add(new SqlParameter("Sex", entity.Sex));
            cmd.Parameters.Add(new SqlParameter("Birth", entity.Birth));
            cmd.Parameters.Add(new SqlParameter("InsurancePolisId", entity.InsurancePolis.Id));
            cmd.Parameters.Add(new SqlParameter("AddressId", entity.Address.Id));
            logger.Trace("SqlCommand для записи данных Пациента в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("PatientDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Пациента id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Patient entity)
        {
            var cmd = new SqlCommand("PatientUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Surname", entity.Surname));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("Middlename", entity.Middlename));
            cmd.Parameters.Add(new SqlParameter("Sex", entity.Sex));
            cmd.Parameters.Add(new SqlParameter("Birth", entity.Birth));
            cmd.Parameters.Add(new SqlParameter("InsurancePolisId", entity.InsurancePolis.Id));
            cmd.Parameters.Add(new SqlParameter("AddressId", entity.Address.Id));
            logger.Trace("SqlCommand для обновления данных Пациента id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetPatient", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Пациента id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            var cmd = new SqlCommand("GetPatientList", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            logger.Trace("SqlCommand для выборки всех существующих Пациентов сформирован");
            return cmd;
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetPatientByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Пациента {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Patient GetEntity(SqlDataReader sqlDataReader)
        {
            Patient patient = new Patient();
            IRepository<InsurancePolis> adoNetRepositoryInsurancePolis = new ADONetRepositoryInsurancePolis();
            IRepository<Address> adoNetRepositoryAddress = new ADONetRepositoryAddress();
            patient.Id = (int)sqlDataReader["Id"];
            patient.Surname = sqlDataReader["Surname"].ToString();
            patient.Name = sqlDataReader["Name"].ToString();
            patient.Middlename = sqlDataReader["Middlename"].ToString();
            patient.Sex = sqlDataReader["Sex"].ToString();
            patient.Birth = (DateTime)sqlDataReader["Birth"];
            patient.InsurancePolis = adoNetRepositoryInsurancePolis.GetById((int)sqlDataReader["InsurancePolisId"]);
            patient.Address = adoNetRepositoryAddress.GetById((int)sqlDataReader["AddressId"]);
            return patient;
        }
    }
}
